package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;



import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:33
 * @description:
 */
@Service
public class UserServiceImp implements UserService {

  @Autowired
  public UserDao dao;
  @Override
  public ServerResponse<User> login(String username, String password) {
    int count = dao.userNameCount(username);
    if (count != 1) {
      return ServerResponse.createError("user do not exist");
    }

    //Todo.password 需要加密
    User user = dao.findByUserNameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("password error");
    }

    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> insert(User user) {

    User us = dao.findByUsername(user.getUsername());

    if (us != null) {
      return ServerResponse.createError("用户已存在");
    }

    dao.insert(user);

    return ServerResponse.createSuccess("校验成功");
  }


  @Override
  public ServerResponse<User> check(String str, String type) {

    if (type.equals("username")) {
      int count = dao.userNameCount(str);
      if (count == 0) {
        return ServerResponse.createSuccess("校验成功");
      }
    }
    if (type.equals("email")) {
      int ccount = dao.userEmailCount(str);
      if (ccount == 0) {
        return ServerResponse.createSuccess("校验成功");
      }
    }
    return ServerResponse.createError("用户已存在");

  }

  @Override
  public ServerResponse<User> getUser(HttpSession session) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      return ServerResponse.createError("用户未登录,无法获取当前用户信息");
    }

    int count = dao.userNameCount(user.getUsername());
    if (count != 1) {
      return ServerResponse.createError("用户不存在");
    }
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> getUserQuestion(User user) {
    int count = dao.userNameCount(user.getUsername());
    String question = dao.getUserQuestion(user.getUsername());
    if (count == 0) {
      return ServerResponse.createError("用户不存在");
    } else {
      if (question == null) {
        return ServerResponse.createError("该用户未设置找回密码问题");
      }
    }
    return ServerResponse.createSuccess(question);
  }



  @Override
  public ServerResponse<User> newPassword(String username, String password) {
    int count = dao.userNameCount(username);
    if (count != 1) {
      return ServerResponse.createError("用户不存在");
    }else{
      int co=dao.newPassword(password);
      if (co==0){
        return ServerResponse.createError("修改密码操作失效");
      }
    }

    return ServerResponse.createSuccess("修改密码成功");
  }

  @Override
  public ServerResponse<User> resetPassword(User user,HttpSession session, String passwordOld, String passwordNew) {
    if(session.getAttribute("user")==null){
      return ServerResponse.createError("请先登录");
    }
    String password=dao.findOldPassword(user);
    if(!password.equals(passwordOld)){
      return ServerResponse.createError("旧密码输入错误");
    }
    user.setPassword(passwordNew);
    int count=dao.resetPassword(user);
    if(count ==1){
      return ServerResponse.createSuccess("修改密码成功");
    }else{
      return ServerResponse.createError("修改密码失败");
    }

  }


  @Override
  public ServerResponse<User> update(User user,HttpSession session) {
    if(session.getAttribute("user")==null){
      return ServerResponse.createError("请先登录");
    }
    int count=dao.update(user);
    if(count==1){
    return ServerResponse.createSuccess("更新个人信息成功");
    }else{
      return ServerResponse.createError("更新个人信息失败");
    }
  }




}